package StreamOperation;

/*
 * Author: JacksonCoder
 * FileName: StreamExample
 * Version: 1.0.0
 * Date: 2025/1/24
 */

import java.util.*;
import java.util.stream.Collectors;

public class StreamExample {

    public static void main(String[] args) {
        List<String> names = Arrays.asList("alen", "alex", "chris", "jack", "james", "jerry", "lucy", "lily", "tom", "tony");

        // 使用Stream 过虑长度大于 4 的名字
        names.stream()
                .filter(name -> name.length() > 4)
                .forEach(System.out::println);

        filterExample();
        mapExample();
        formatExample();
        SortedExample();
        DistinctExample();
        ListExample();
        SkipExample();
    }

    private static void filterExample() {
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

        numbers.stream()
                .filter(number -> number % 2 == 0)
                .forEach(System.out::println);
    }

    private static void mapExample() {
        List<String> names = Arrays.asList("alen", "alex", "chris", "jack", "james", "jerry", "lucy", "lily", "tom", "tony");

        names.stream()
                .map(String::toUpperCase)
                .forEach(System.out::println);
    }

    private static void formatExample() {
        List<List<String>> nestedName = Arrays.asList(
                Arrays.asList("Alice", "Bob"),
                Arrays.asList("Charlie", "David"),
                Arrays.asList("Eve")
        );

        // 将嵌套的集合扁平化成为一个流
        List<String> names = nestedName.stream()
                .flatMap(List::stream)
                .collect(Collectors.toList());

        System.out.println(names);
    }

    private static void SortedExample() {
        List<String> names = Arrays.asList("james", "bob", "alen", "chris", "jack", "jerry", "lucy", "alex", "lily", "tom", "tony");

        names.stream()
                .sorted()
                .forEach(System.out::println);
    }

    private static void DistinctExample() {
        List<String> names = Arrays.asList("Alice", "Bob", "Charlie", "Alice", "Bob");

        names.stream()
                .distinct()
                .forEach(System.out::println);
    }

    private static void ListExample() {
        List<String> names = Arrays.asList("james", "bob", "alen", "chris", "jack", "jerry", "lucy", "alex", "lily", "tom", "tony");

        names.stream()
                .limit(3)
                .forEach(System.out::println);

    }

    private static void SkipExample() {
        List<String> names = Arrays.asList("james", "bob", "alen", "chris", "jack", "jerry", "lucy", "alex", "lily", "tom", "tony");

        names.stream()
                .skip(3)
                .forEach(System.out::println);
    }



}
